/*
 * @(#) TestConnectQLight.java 2013-6-6 上午11:28:56
 *
 * Copyright 2013 Rockwell Automation, Inc. All rights reserved.
 * Rockwell Automation PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package com.osi.mes.pm;

import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;

public class TestConnectQLight
{
	public TestConnectQLight()
	{

	}

	static
	{
		Thread loadThread = new Thread()
		{
			public void run()
			{
				System.out.println("loadLibrary QUvc_dll ..." + Thread.currentThread().getName());
				System.loadLibrary("QUvc_dll");
			}
		};
		loadThread.start();
	}

	/**
	 * Initialize various parameters and handle related to USB tower lamp.
	 * 
	 * Usb_Qu_Open
	 * 
	 * @throws NativeException
	 * @throws IllegalAccessException
	 */
	private static void Usb_Qu_Open()
		throws NativeException, IllegalAccessException
	{
		JNative n = null;
		try
		{
			n = new JNative("QUvc_dll.dll", "Usb_Qu_Open");
			n.setRetVal(Type.VOID);
			n.invoke();
		}
		finally
		{
			if (n != null)
				n.dispose();
		}
	}

	/**
	 * Clear handles related to USB tower lamp. It should be run when exit.
	 * 
	 * Usb_Qu_Close
	 * 
	 * @throws NativeException
	 * @throws IllegalAccessException
	 */
	private static void Usb_Qu_Close()
		throws NativeException, IllegalAccessException
	{
		JNative n = null;
		try
		{
			n = new JNative("QUvc_dll.dll", "Usb_Qu_Close");
			n.setRetVal(Type.VOID);
			n.invoke();
		}
		finally
		{
			if (n != null)
				n.dispose();
		}
	}

	/**
	 * It is used to confirm whether USB tower light is connected or
	 * disconnected. Making sure by confirmation Return Value of Bit.
	 * 
	 * Usb_Qu_Getstate
	 * 
	 * @return int 1 :成功 0:失败
	 * @throws NativeException
	 * @throws IllegalAccessException
	 */
	private static int Usb_Qu_Getstate()
		throws NativeException, IllegalAccessException
	{
		JNative n = null;
		try
		{
			n = new JNative("QUvc_dll.dll", "Usb_Qu_Getstate");
			n.setRetVal(Type.INT);
			n.invoke();
			// int retVal = Integer.parseInt(n.getRetVal());
			return Integer.parseInt(n.getRetVal());
		}
		finally
		{
			if (n != null)
				n.dispose();
		}
	}

	/**
	 * Turn on or off the signal light and soundf of USB tower lamp.
	 * 
	 * Usb_Qu_Getstate
	 * 
	 * @param Qu_index
	 *            (0-3) : TOWER LAMP index number
	 * @param Qu_type
	 *            (0-4) Tower Lamp with 8 harmony WM or WA and Buzzer : (0)
	 *            Tower Lamp with WS, WP, WB(buzzer sound from speaker) and WM
	 *            or WA without harmony : (0-4)
	 * @param pData
	 *            : Lamp and Sound Control Data Point byte bAcon[6]; bAcon[0]
	 *            ~bAcon[4] : Lamp Control data(0-2) 0-OFF, 1-ON.
	 *            2-blink(on/off), Else –Don’t change before state bAcon[0] –
	 *            Red Lamp bAcon[1] – Yellow Lamp bAcon[2] – Green Lamp bAcon[3]
	 *            – Blue Lamp bAcon[4] – White Lamp bAcon[5] : Sound Contro
	 *            data(0-5)
	 * 
	 * @return int 1 :成功 0:失败
	 * @throws NativeException
	 * @throws IllegalAccessException
	 */
	private static int Usb_Qu_write(byte Qu_index, byte Qu_type, byte[] bData)
		throws NativeException, IllegalAccessException
	{
		JNative n = null;
		try
		{
			n = new JNative("QUvc_dll.dll", "Usb_Qu_write");
			n.setRetVal(Type.INT);
			int i = 0;
			n.setParameter(
				i++, Type.INT, "" + Qu_index);
			n.setParameter(
				i++, Type.INT, "" + Qu_type);

			// 为数组创建空间
			Pointer aArrIntInput = new Pointer(MemoryBlockFactory.createMemoryBlock(bData.length));
			for (int j = 0; j < bData.length; j++)
			{
				aArrIntInput.setByteAt(
					j, bData[j]);
			}

			// 设定传入参数
			n.setParameter(
				i++, aArrIntInput); // 数组指针
			n.setParameter(
				i++, bData.length); // 数组大小

			// 设定返回类型
			n.setRetVal(Type.INT);

			// 调用DLL
			n.invoke();

			return Integer.parseInt(n.getRetVal());
		}
		finally
		{
			if (n != null)
				n.dispose();
		}
	}

	/*
	 * 打开塔灯
	 */
	public static boolean openTowerLamp()
	{
		boolean flag = true;
		try
		{
			Usb_Qu_Open();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}

	/*
	 * 关闭塔灯
	 */
	public static boolean closeTowerLamp()
	{
		boolean flag = true;
		try
		{
			Usb_Qu_Close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}

	/*
	 * 当前是否连接状态
	 */
	public static boolean isConnected()
	{
		boolean flag = false;
		try
		{
			int state = Usb_Qu_Getstate();
			if (state == 1)
				flag = true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return flag;
	}

	/*
	 * 写红灯报警常亮
	 */
	public static boolean writeRedLightOn()
	{
		boolean flag = false;
		if (isConnected())
		{
			try
			{
				byte[] bData = new byte[6];
				byte C_index = 0;
				byte C_type = 0;

				byte D_not = 100;
				byte C_lampoff = 0;
				byte C_lampon = 1;
				byte C_lampblink = 2;
				bData[0] = C_lampon;
				bData[1] = C_lampoff;
				bData[2] = C_lampoff;
				bData[3] = C_lampoff;
				bData[4] = C_lampoff;
				bData[5] = 0;

				int state = Usb_Qu_write(
					C_index, C_type, bData);
				if (state == 1)
					flag = true;
				else
					flag = false;
			}
			catch (Exception e)
			{
				e.printStackTrace();
				flag = false;
			}
		}
		return flag;
	}

	/*
	 * 写红灯报警一闪一闪
	 */
	public static boolean writeRedLightOnOff()
	{
		boolean flag = false;
		if (isConnected())
		{
			try
			{
				byte[] bData = new byte[6];
				byte C_index = 0;
				byte C_type = 0;

				byte D_not = 100;
				byte C_lampoff = 0;
				byte C_lampon = 1;
				byte C_lampblink = 2;
				bData[0] = C_lampblink;
				bData[1] = C_lampoff;
				bData[2] = C_lampoff;
				bData[3] = C_lampoff;
				bData[4] = C_lampoff;
				bData[5] = 0;

				int state = Usb_Qu_write(
					C_index, C_type, bData);
				if (state == 1)
					flag = true;
				else
					flag = false;
			}
			catch (Exception e)
			{
				e.printStackTrace();
				flag = false;
			}
		}
		return flag;
	}

	/*
	 * 写黄灯报警常亮
	 */
	public static boolean writeYellowLightOn()
	{
		boolean flag = false;
		if (isConnected())
		{
			try
			{
				byte[] bData = new byte[6];
				byte C_index = 0;
				byte C_type = 0;

				byte D_not = 100;
				byte C_lampoff = 0;
				byte C_lampon = 1;
				byte C_lampblink = 2;
				bData[0] = C_lampoff;
				bData[1] = C_lampon;
				bData[2] = C_lampoff;
				bData[3] = C_lampoff;
				bData[4] = C_lampoff;
				bData[5] = 0;

				int state = Usb_Qu_write(
					C_index, C_type, bData);
				if (state == 1)
					flag = true;
				else
					flag = false;
			}
			catch (Exception e)
			{
				e.printStackTrace();
				flag = false;
			}
		}
		return flag;
	}

	/*
	 * 写黄灯报警一闪一闪
	 */
	public static boolean writeYellowLightOnOff()
	{
		boolean flag = false;
		if (isConnected())
		{
			try
			{
				byte[] bData = new byte[6];
				byte C_index = 0;
				byte C_type = 0;

				byte D_not = 100;
				byte C_lampoff = 0;
				byte C_lampon = 1;
				byte C_lampblink = 2;
				bData[0] = C_lampoff;
				bData[1] = C_lampblink;
				bData[2] = C_lampoff;
				bData[3] = C_lampoff;
				bData[4] = C_lampoff;
				bData[5] = 0;

				int state = Usb_Qu_write(
					C_index, C_type, bData);
				if (state == 1)
					flag = true;
				else
					flag = false;
			}
			catch (Exception e)
			{
				e.printStackTrace();
				flag = false;
			}
		}
		return flag;
	}

	/*
	 * 写绿灯常亮
	 */
	public static boolean writeGreenLightOn()
	{

		boolean flag = false;
		if (isConnected())
		{
			try
			{
				byte[] bData = new byte[6];
				byte C_index = 0;
				byte C_type = 0;

				byte D_not = 100;
				byte C_lampoff = 0;
				byte C_lampon = 1;
				byte C_lampblink = 2;
				bData[0] = C_lampoff;
				bData[1] = C_lampoff;
				bData[2] = C_lampon;
				bData[3] = C_lampoff;
				bData[4] = C_lampoff;
				bData[5] = 0;

				int state = Usb_Qu_write(
					C_index, C_type, bData);
				if (state == 1)
					flag = true;
				else
					flag = false;
			}
			catch (Exception e)
			{
				e.printStackTrace();
				flag = false;
			}
		}
		return flag;
	}

	public static void main(String[] args)
	{
		writeRedLightOn();
	}
}
